<?php
namespace app\index\controller;

use think\Request;

class Account extends Common{

	//收款账号列表
	public function index(){
		
		//$response = \ServerHelper::post('account_list',['type'=>'list','agentid'=>'AG_000000']);
		$response = \ServerHelper::post('account_action',['type'=>'list','agentid'=>AGENT_ID]);
		//var_dump($response);
		$data = $this->J($response);
		
		$list = [];
		
		foreach($data['data']['accountinfo'] as $one){
			$list['type'.$one['accounttype']] = $one;
		}
		
		$list['banklist'] = $data['data']['banklist'];

		
		return view('',$list);
	}
	
	//保存微信
	public function wechat_save(){
		
		$request = Request::instance();
		
		$param = [
			'type' => 'save',
			'agentid' => AGENT_ID,
			
			'accounttype' => ACCOUNT_TYPE_WECHAT,
			'accountname' => $request->param('wechatname'),
			'accountid' => $request->param('wechataccount'),
			'accoutpicture' => $request->param('accountpicture'),
			'password' => $request->param('password'),
			/*
			'bankname': Q('bankname'),
			'bankaddress': Q('bankaddress'),
			'bankbranchname': Q('bankbranchname')
			*/
		];
		if($param['accountname'] == ''){
			ajax('请输入用户名');
		}
		if($param['accountid'] == ''){
			ajax('请输入微信账号');
		}
		if($param['accoutpicture'] == ''){
			ajax('请选上传款码');
		}
		if($param['password'] == ''){
			ajax('请输入登陆密码');
		}
		
		$response = \ServerHelper::post('account_action',$param);
		//var_dump($response);
		
		$data = $this->J($response);
		if(!isset($data['flag'])){
			ajax('保存失败');
		}
		if($data['flag'] == '0'){ ajax($data['msg']); }
		
		ajax('保存成功',true);
		
		
		
	}
	
	//保存支付宝
	public function alipay_save(){
		
		$request = Request::instance();
		
		$param = [
			'type' => 'save',
			'agentid' => AGENT_ID,
			
			'accounttype' => ACCOUNT_TYPE_ALIPAY,
			'accountname' => $request->param('alipay_accountname'),
			'accountid' => $request->param('alipay_accountid'),
			'password' => $request->param('password'),
			/*
			'bankname': Q('bankname'),
			'bankaddress': Q('bankaddress'),
			'bankbranchname': Q('bankbranchname')
			*/
		];
		if($param['accountname'] == ''){
			ajax('请输入用户名');
		}
		if($param['accountid'] == ''){
			ajax('请输入支付宝账号');
		}
		if($param['password'] == ''){
			ajax('请输入登陆密码');
		}
		
		$response = \ServerHelper::post('account_action',$param);
		
		$data = $this->J($response);
		if(!isset($data['flag'])){
			ajax('保存失败');
		}
		if($data['flag'] == '0'){ ajax($data['msg']); }
		
		ajax('保存成功',true);
		

	}
	
	
	public function bank_save(){
		/*
		'bankname' : $("#bankname").val(),
		'bankaddress' : $("#bankaddress").val(),
		'bankbranchname' : $("#bankbranchname").val(),
		'bank_accountname' : $("#bank_accountname").val(),
		'bank_accountid' : $("#bank_accountid").val(),
		'password' : $('#confirmpwd').val()
		*/
		
		$request = Request::instance();
		
		$param = [
			'type' => 'save',
			'agentid' => AGENT_ID,
			'accounttype' => ACCOUNT_TYPE_BANK,
			'accountname' => $request->param('bank_accountname'),
			'accountid' => $request->param('bank_accountid'),
			'password' => $request->param('password'),
			
			'bankname' => $request->param('bankname'),
			'bankaddress' => $request->param('bankaddress'),
			'bankbranchname' => $request->param('bankbranchname'),
		];
		if($param['bankname'] == ''){
			ajax('请选择开户行');
		}
		if($param['bankaddress'] == ''){
			ajax('请选择开户地址');
		}
		if($param['bankbranchname'] == ''){
			ajax('请输入开户网点');
		}
		if($param['accountname'] == ''){
			ajax('请输入姓名');
		}
		if($param['accountid'] == ''){
			ajax('请输入卡号');
		}
		if($param['password'] == ''){
			ajax('请输入登陆密码');
		}
		
		$response = \ServerHelper::post('account_action',$param);
		
		$data = $this->J($response);
		if(!isset($data['flag'])){
			ajax('保存失败');
		}
		if($data['flag'] == '0'){ ajax($data['msg']); }
		
		ajax('保存成功',true);

	}
	
	//删除账号
	public function account_delete(){
		$request = Request::instance();
		
		$param = [
			'type' => 'del',
			'agentid' => AGENT_ID,
			'accounttype' => $request->param('accounttype'),
			'password' => $request->param('password'),
		];
		$response = \ServerHelper::post('account_action',$param);
		
		$data = $this->J($response);
		if(!isset($data['flag'])){
			ajax('删除失败');
		}
		if($data['flag'] == '0'){ ajax($data['msg']); }
		
		ajax('删除成功',true);
		
		
	}
	

}
